/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.imagelettericon.slice;

import com.imagelettericon.AltContactsFragment;
import com.imagelettericon.AltCountriesFragment;
import com.imagelettericon.ContactsFragment;
import com.imagelettericon.CountriesFragment;
import com.imagelettericon.PopupDialogMenu;
import com.imagelettericon.ResourceTable;
import com.next.androidintentlibrary.BrowserIntents;
import com.next.androidintentlibrary.ShareIntents;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;

/**
 * MainAbilitySlice
 *
 * @since 2021-06-02
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final int WIDTH_DIALOG = 210;

    private static final int HEIGHT_DIALOG = 340;

    private static final int WIDTH_COMMON = 1000;

    private static final int HEIGHT_COMMON = 300;

    private static final int WIDTH_ARROW = 20;

    private static final int HEIGHT_ARROW = 20;

    private static final int RADIUS = 20;

    private PopupDialogMenu mPopupDialogMenu;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(Color.getIntColor("#3F51B5"));
        ((FractionAbility) getAbility()).getFractionManager().startFractionScheduler()
                .add(ResourceTable.Id_stack, new ContactsFragment()).submit();
        initView();
    }

    private void initView() {
        findComponentById(ResourceTable.Id_title_menu).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Component anchorView = findComponentById(ResourceTable.Id_title_menu);
                Component componentMenu = LayoutScatter.getInstance(getContext())
                        .parse(ResourceTable.Layout_menu_popup_dialog, null, false);
                showTransformersTip(anchorView, componentMenu);
            }
        });
    }

    private void showTransformersTip(Component anchorView, Component component) {
        Component.ClickedListener clickedListener = new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mPopupDialogMenu.destroy();
                componentClick(component.getId());
            }
        };

        mPopupDialogMenu = new PopupDialogMenu(anchorView, component) {
            @Override
            public boolean isTouchOutside() {
                dismissTip();
                return false;
            }

            @Override
            public void initView(Component contentView) {
                contentView.findComponentById(ResourceTable.Id_action_show_contacts)
                        .setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_show_countries)
                        .setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_show_alternate_contacts)
                        .setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_show_alternate_countries)
                        .setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_play_store)
                        .setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_other_apps)
                        .setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_github)
                        .setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_share)
                        .setClickedListener(clickedListener);
            }
        };
        mPopupDialogMenu.setSize(AttrHelper.vp2px(WIDTH_DIALOG, getContext()),
                AttrHelper.vp2px(HEIGHT_DIALOG, getContext()));
        mPopupDialogMenu.setMode(LayoutAlignment.BOTTOM);
        mPopupDialogMenu.setBgColor(new Color(Color.getIntColor("#00EEEEEE")));
        mPopupDialogMenu.setHasArrow(true);
        mPopupDialogMenu.setCornerRadius(RADIUS);
        mPopupDialogMenu.setAutoClosable(true);
        mPopupDialogMenu.setTransparent(true);
        mPopupDialogMenu.setArrowSize(WIDTH_ARROW, HEIGHT_ARROW);
        mPopupDialogMenu.showDialog();
    }

    private void componentClick(int id) {
        Fraction fraction = new ContactsFragment();
        boolean isOpenFragment = false;
        switch (id) {
            case ResourceTable.Id_action_show_contacts:
                fraction = new ContactsFragment();
                isOpenFragment = true;
                break;
            case ResourceTable.Id_action_show_countries:
                fraction = new CountriesFragment();
                isOpenFragment = true;
                break;
            case ResourceTable.Id_action_show_alternate_contacts:
                fraction = new AltContactsFragment();
                isOpenFragment = true;
                break;
            case ResourceTable.Id_action_show_alternate_countries:
                fraction = new AltCountriesFragment();
                isOpenFragment = true;
                break;
            case ResourceTable.Id_action_play_store:
                BrowserIntents.from(this).openLink(getString(ResourceTable.String_intents_imagelettericon)).show();
                break;
            case ResourceTable.Id_action_other_apps:
                BrowserIntents.from(this).openLink(getString(ResourceTable.String_intents_akashjain)).show();
                break;
            case ResourceTable.Id_action_github:
                BrowserIntents.from(this).openLink(getString(ResourceTable.String_intents_git)).show();
                break;
            case ResourceTable.Id_action_share:
                ShareIntents.from(this).shareText("Material Icon", getString(ResourceTable.String_intents_share1)
                        + getString(ResourceTable.String_intents_share2)).show();
                break;
            default:
                break;
        }
        if (isOpenFragment) {
            ((FractionAbility) getAbility()).getFractionManager().startFractionScheduler().
                    replace(ResourceTable.Id_stack, fraction).submit();
        }
    }

    @Override
    public void onBackPressed() {
        showDialog();
    }

    private void showDialog() {
        CommonDialog commonDialog = new CommonDialog(this);
        Component component = LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_dialog_custom, null, true);
        component.findComponentById(ResourceTable.Id_yes).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getAbility().terminateAbility();
                commonDialog.hide();
            }
        });
        component.findComponentById(ResourceTable.Id_no).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                commonDialog.hide();
            }
        });
        component.findComponentById(ResourceTable.Id_rateus).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                BrowserIntents.from(getContext()).openLink(getString(ResourceTable.String_intents_imagelettericon))
                        .show();
                commonDialog.hide();
            }
        });
        commonDialog.setSize(WIDTH_COMMON, HEIGHT_COMMON);
        commonDialog.setContentCustomComponent(component);
        commonDialog.setAutoClosable(true);
        commonDialog.show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
